/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.ejb.review.impl;

import com.nus.application.ejb.review.ReviewAssignmentEjbLocal;
import com.nus.constant.ConstantStatus;
import com.nus.dataaccess.dao.review.ReviewAssignment;
import com.nus.dataaccess.dao.review.ReviewResult;
import com.nus.dataaccess.dao.review.dao.ReviewAssignmentDao;
import com.nus.dataaccess.dao.review.dao.ReviewResultDao;
import com.nus.dataaccess.dao.util.HibernateUtil;
import com.nus.util.BeanUtil;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;

/**
 *
 * @author robin.foe
 */
@Stateless
@PermitAll
public class ReviewAssignmentEjb implements ReviewAssignmentEjbLocal{
    
    @Override
    public List<ReviewAssignment> paginate(int first, int pageSize, String sortField, boolean ascending, Map<String, Object> filters){
        return ReviewAssignmentDao.getInstance().paginate(first, pageSize, sortField, ascending, filters);
    }

    @Override
    public int countTotal(Map<String, Object> filters){
        return ReviewAssignmentDao.getInstance().getTotalCount(filters);
    }
    
    @Override
    public ReviewAssignment getReviewAssignment(Long id){
        return ReviewAssignmentDao.getInstance().get(id);
    }
    
    @Override
    public void persistReviewResult(ReviewResult reviewResult){
        try {
            if(!reviewResult.isPkSet()){
                ReviewResultDao.getInstance().save(reviewResult);
            }else{
                    ReviewResult dbReviewResult = ReviewResultDao.getInstance().get(reviewResult.getId());
                    BeanUtil.copyProperties(dbReviewResult, reviewResult, "id", "reviewAssignment" , "reviewAssignmentId");
                    ReviewResultDao.getInstance().saveOrUpdate(reviewResult);
            }
        } catch (Exception ex) {
                ex.printStackTrace();
        }
    }
    
    @Override
    public void saveReviewResult(ReviewResult reviewResult){
        try {
            HibernateUtil.beginTransaction();
            
            ReviewAssignment reviewAssignment = ReviewAssignmentDao.getInstance().get(reviewResult.getReviewAssignmentId());
            reviewAssignment.setReviewStatus(ConstantStatus.REV_STS_INPROGRESS);
            ReviewAssignmentDao.getInstance().saveOrUpdate(reviewAssignment);
            
            if(!reviewResult.isPkSet()){
                ReviewResultDao.getInstance().save(reviewResult);
            }else{
                    ReviewResult dbReviewResult = ReviewResultDao.getInstance().get(reviewResult.getId());
                    BeanUtil.copyProperties(dbReviewResult, reviewResult, "id", "reviewAssignment" , "reviewAssignmentId");
                    ReviewResultDao.getInstance().saveOrUpdate(dbReviewResult);
            }
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            HibernateUtil.rollbackTransaction();
                ex.printStackTrace();
        }finally{
            HibernateUtil.closeSession();
         }
    }
    
    @Override
    public void completeReviewResult(ReviewResult reviewResult){
        try {
            HibernateUtil.beginTransaction();
            
            ReviewAssignment reviewAssignment = ReviewAssignmentDao.getInstance().get(reviewResult.getReviewAssignmentId());
            reviewAssignment.setReviewStatus(ConstantStatus.REV_STS_COMPLETED);
            ReviewAssignmentDao.getInstance().saveOrUpdate(reviewAssignment);
            
            if(!reviewResult.isPkSet()){
                ReviewResultDao.getInstance().save(reviewResult);
            }else{
                    ReviewResult dbReviewResult = ReviewResultDao.getInstance().get(reviewResult.getId());
                    BeanUtil.copyProperties(dbReviewResult, reviewResult, "id", "reviewAssignment" , "reviewAssignmentId");
                    ReviewResultDao.getInstance().saveOrUpdate(dbReviewResult);
            }
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            HibernateUtil.rollbackTransaction();
                ex.printStackTrace();
        }finally{
            HibernateUtil.closeSession();
        }
    }
}
